<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description">
<meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://yoursite.com/page/10/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Hexo">
<meta name="twitter:description">
  
    <link rel="alternative" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  

</head>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-nginx/3.HAproxy配置" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/17/nginx/3.HAproxy配置/" class="article-date">
  <time datetime="2015-12-16T19:00:00.000Z" itemprop="datePublished">2015-12-17</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/nginx/">nginx</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/17/nginx/3.HAproxy配置/">HAproxy配置</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u7B80_u4ECB"><a href="#u7B80_u4ECB" class="headerlink" title="简介"></a>简介</h2><p>　　与nginx类似，haproxy也作为一个应用服务器，原生提供了tcp和http协议支持。这点与nginx不同，nginx需要编译第三方扩展支持。lvs则作为4层协议负载。<br>　　haproxy可以作为mysql的负载均衡，实现读写分离，从库负载均衡（反向代理）</p>
<h2 id="u5B89_u88C5"><a href="#u5B89_u88C5" class="headerlink" title="安装"></a>安装</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">tar zxvf haproxy-<span class="number">1.5</span>.<span class="number">4</span>.tar.gz</span><br><span class="line"><span class="built_in">cd</span> haproxy-<span class="number">1.5</span>.<span class="number">4</span></span><br><span class="line">uname <span class="operator">-a</span> //查看linux内核版本</span><br><span class="line">make TARGET=linux26 PREFIX=/shihc/haproxy-bin</span><br><span class="line">make install PREFIX=/shihc/haproxy-bin</span><br></pre></td></tr></table></figure>
<p>　　编辑/shihc/haproxy-bin/conf/haproxy.conf，调整为如下配置<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># this config needs haproxy-1.1.28 or haproxy-1.2.1</span></span><br><span class="line"></span><br><span class="line">global</span><br><span class="line">	<span class="built_in">log</span> <span class="number">127.0</span>.<span class="number">0.1</span>	<span class="built_in">local</span>0</span><br><span class="line">	<span class="built_in">log</span> <span class="number">127.0</span>.<span class="number">0.1</span>	<span class="built_in">local</span>1 notice</span><br><span class="line">	<span class="comment">#log loghost	local0 info</span></span><br><span class="line">	maxconn <span class="number">4096</span></span><br><span class="line">	chroot /usr/share/haproxy</span><br><span class="line">	pidfile /shihc/haproxy-bin/run/haproxy.pid   <span class="comment">#注意此处配置了pid的位置</span></span><br><span class="line">	uid <span class="number">99</span></span><br><span class="line">	gid <span class="number">99</span></span><br><span class="line">	daemon</span><br><span class="line">	<span class="comment">#debug</span></span><br><span class="line">	<span class="comment">#quiet</span></span><br><span class="line"></span><br><span class="line">defaults</span><br><span class="line">	<span class="built_in">log</span>	global</span><br><span class="line">	<span class="built_in">log</span> <span class="number">127.0</span>.<span class="number">0.1</span> <span class="built_in">local</span>3 debug <span class="comment">#日志级别[err warning info debug]</span></span><br><span class="line">	mode	http</span><br><span class="line">	option	httplog</span><br><span class="line">	option	dontlognull</span><br><span class="line">	retries	<span class="number">3</span></span><br><span class="line">	<span class="comment"># redispatch</span></span><br><span class="line">	maxconn	<span class="number">2000</span></span><br><span class="line">	contimeout	<span class="number">5000</span></span><br><span class="line">	<span class="comment"># clitimeout	50000</span></span><br><span class="line">	<span class="comment"># srvtimeout	50000</span></span><br><span class="line">	</span><br><span class="line">listen cluster <span class="number">0.0</span>.<span class="number">0.0</span>:<span class="number">9999</span> <span class="comment">#运行的端口及主机名</span></span><br><span class="line">	mode http <span class="comment">#使用http的7层模式</span></span><br><span class="line">	balance roundrobin <span class="comment">#设置服务器负载分配算法</span></span><br><span class="line">	option httpclose</span><br><span class="line">	option forwardfor</span><br><span class="line">	option httpchk GET /keepalive.html <span class="comment">#健康检测页面</span></span><br><span class="line">	stats uri /admins <span class="comment">#监控页面的访问地址</span></span><br><span class="line"></span><br><span class="line">listen cluster2 <span class="number">0.0</span>.<span class="number">0.0</span>:<span class="number">9998</span> <span class="comment">#运行的端口及主机名</span></span><br><span class="line">	mode http <span class="comment">#使用http的7层模式</span></span><br><span class="line">	balance roundrobin <span class="comment">#设置服务器负载分配算法</span></span><br><span class="line">	option httpclose</span><br><span class="line">	option forwardfor</span><br><span class="line">	server push-html <span class="number">10.10</span>.<span class="number">51.31</span>:<span class="number">8081</span>/push-html weight <span class="number">1</span> check inter <span class="number">2000</span> rise <span class="number">2</span> fall <span class="number">3</span></span><br><span class="line">	server examples <span class="number">10.10</span>.<span class="number">51.31</span>:<span class="number">8081</span>/examples weight <span class="number">1</span> check inter <span class="number">2000</span> rise <span class="number">2</span> fall <span class="number">3</span></span><br><span class="line">	<span class="comment"># weight - 调节服务器的负重</span></span><br><span class="line">	<span class="comment"># check - 允许对该服务器进行健康检查</span></span><br><span class="line">	<span class="comment"># inter - 设置连续的两次健康检查之间的时间，单位为毫秒(ms)，默认值 2000(ms)</span></span><br><span class="line">	<span class="comment"># rise - 指定多少次连续成功的健康检查后，即可认定该服务器处于可操作状态，默认值 2</span></span><br><span class="line">	<span class="comment"># fall - 指定多少次不成功的健康检查后，认为服务器为当掉状态，默认值 3</span></span><br><span class="line">	<span class="comment"># maxconn - 指定可被发送到该服务器的最大并发连接数</span></span><br></pre></td></tr></table></figure></p>
<h2 id="u7F16_u5199_u670D_u52A1_u811A_u672C"><a href="#u7F16_u5199_u670D_u52A1_u811A_u672C" class="headerlink" title="编写服务脚本"></a>编写服务脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir /shihc/haproxy-bin/init.d/</span><br><span class="line">mkdir -p /shihc/haproxy-bin/run/</span><br></pre></td></tr></table></figure>
<p>　　编写启动脚本：/shihc/haproxy-bin/init.d/haproxy<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="shebang">#! /bin/sh</span><br><span class="line"></span></span><br><span class="line">PROGDIR=/shihc/haproxy-bin</span><br><span class="line">PROGNAME=haproxy</span><br><span class="line">DAEMON=<span class="variable">$PROGDIR</span>/sbin/<span class="variable">$PROGNAME</span></span><br><span class="line">CONFIG=<span class="variable">$PROGDIR</span>/conf/<span class="variable">$PROGNAME</span>.conf</span><br><span class="line">PIDFILE=<span class="variable">$PROGDIR</span>/run/<span class="variable">$PROGNAME</span>.pid</span><br><span class="line">DESC=<span class="string">"HAProxy daemon"</span></span><br><span class="line">SCRIPTNAME=/shihc/haproxy-bin/init.d/<span class="variable">$PROGNAME</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Gracefully exit if the package has been removed.</span></span><br><span class="line"><span class="built_in">test</span> -x <span class="variable">$DAEMON</span> || <span class="built_in">exit</span> <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">start</span></span>()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">echo</span> -n <span class="string">"Starting <span class="variable">$DESC</span>: <span class="variable">$PROGNAME</span>"</span></span><br><span class="line">    <span class="variable">$DAEMON</span> <span class="operator">-f</span> <span class="variable">$CONFIG</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"."</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">stop</span></span>()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">echo</span> -n <span class="string">"Stopping <span class="variable">$DESC</span>: <span class="variable">$PROGNAME</span>"</span></span><br><span class="line">    cat <span class="variable">$PIDFILE</span> | xargs <span class="built_in">kill</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"."</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></span><br><span class="line">  start)</span><br><span class="line">    start</span><br><span class="line">    ;;</span><br><span class="line">  stop)</span><br><span class="line">    stop</span><br><span class="line">    ;;</span><br><span class="line">*)</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$SCRIPTNAME</span> &#123;start|stop&#125;"</span> &gt;&amp;<span class="number">2</span></span><br><span class="line">    <span class="built_in">exit</span> <span class="number">1</span></span><br><span class="line">    ;;</span><br><span class="line"><span class="keyword">esac</span></span><br><span class="line"><span class="built_in">exit</span> <span class="number">0</span></span><br></pre></td></tr></table></figure></p>
<h2 id="u6267_u884C_u670D_u52A1_u811A_u672C"><a href="#u6267_u884C_u670D_u52A1_u811A_u672C" class="headerlink" title="执行服务脚本"></a>执行服务脚本</h2><p>/shihc/haproxy-bin/init.d/haproxy start<br>停止服务：<br>/shihc/haproxy-bin/init.d/haproxy stop</p>
<h2 id="u8BBF_u95EE"><a href="#u8BBF_u95EE" class="headerlink" title="访问"></a>访问</h2><p>统计页面：<a href="http://192.168.85.65:9999/admins" target="_blank" rel="external">http://192.168.85.65:9999/admins</a><br>push-html页面：<a href="http://192.168.85.65:9998/push-html/" target="_blank" rel="external">http://192.168.85.65:9998/push-html/</a><br>examples页面：<a href="http://192.168.85.65:9998/examples/" target="_blank" rel="external">http://192.168.85.65:9998/examples/</a></p>
<h2 id="u914D_u7F6E_u65E5_u5FD7_28_u5B9E_u9645_u672A_u6253_u5370_u65E5_u5FD7_29"><a href="#u914D_u7F6E_u65E5_u5FD7_28_u5B9E_u9645_u672A_u6253_u5370_u65E5_u5FD7_29" class="headerlink" title="配置日志(实际未打印日志)"></a>配置日志(实际未打印日志)</h2><p>mkdir /shihc/haproxy-bin/log<br>touch /shihc/haproxy-bin/log/haproxy.log</p>
<p>centos6中配置方法</p>
<p>vim /etc/sysconfig/rsyslog<br>SYSLOGD_OPTIONS=”-m 0 -r -x”</p>
<p>vim /etc/rsyslog.conf<br>local3.* /mapbar/haproxy-bin/log/haproxy.log</p>
<p>/etc/init.d/rsyslog restart</p>
<p>重复第四步</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/17/nginx/3.HAproxy配置/" data-id="cijzzpv86006m1cfshl9ni77c" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/nginx/">nginx</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-nginx/2.Nginx系列之反向代理" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/17/nginx/2.Nginx系列之反向代理/" class="article-date">
  <time datetime="2015-12-16T18:00:00.000Z" itemprop="datePublished">2015-12-17</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/nginx/">nginx</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/17/nginx/2.Nginx系列之反向代理/">Nginx系列二之反向代理</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u53CD_u5411_u4EE3_u7406"><a href="#u53CD_u5411_u4EE3_u7406" class="headerlink" title="反向代理"></a>反向代理</h2><p>　　服务器代理网络上的客户机请求，将请求转达给内部真实服务器，然后再返回给Internet客户端，代理服务器上面没有任何网页资料。反向代理和正向代理没有冲突，可以在防火墙设备中同时使用这两种结合，正向代理可以进行过滤，保护内部网络安全。软件七层负载均衡大多是基于HTTP反向代理，Nginx反向代理能够支持虚拟主机，可以按照轮询，IP哈希，URL哈希，权重方式对后端服务器做负载均衡，同时还支持热备（failover）的功能，还支持后端服务器健康检查。<br>　　反向代理的典型用途是将防火墙后面的服务器提供给Internet用户访问。反向代理还可以为后端的多台服务器提供负载平衡，或为后端较慢的服务器提供缓冲服务。</p>
<p>资料下载：<a href="/code/第6章 Nginx HTTP负载均衡和反向代理的配置与优化.pdf">Nginx HTTP负载均衡和反向代理的配置与优化.pdf</a></p>
<h2 id="u4EE3_u7406_u79CD_u7C7B"><a href="#u4EE3_u7406_u79CD_u7C7B" class="headerlink" title="代理种类"></a>代理种类</h2><p>1.硬件四/七负载均衡设备<br>2.软件四层负载设备，代表作品为LVS(Linux Virtual Server)<br>3.软件七层负载均衡，代表产品有Nginx，L7SW，HAProxy</p>
<h2 id="u5206_u914D_u65B9_u5F0F"><a href="#u5206_u914D_u65B9_u5F0F" class="headerlink" title="分配方式"></a>分配方式</h2><p>　　1. 轮询（默认）：每个请求按时间顺序逐一分配到后端服务器。如果后端服务器down掉，能自动剔除，但如果一台服务器发生故障，那么所有的访问该服务器的请求将不会有所回应<br>upstream test{<br>    server 192.168.0.1:3000;<br>    server 192.168.0.1:3001;<br>}</p>
<p>　　2. weight：指定轮询几率，weight和访问比率成正比，用于后端服务器性能不均的情况<br>upstream test{<br>    server 192.168.0.1 weight=2;<br>    server 192.168.0.2 weight=3;<br>}</p>
<p>　　3. ip_hash：每个请求按访问ip的hash结果分配，这样每个访客固定访问一个后端服务器，可以解决session的问题？？？？（好像不行）<br>upstream test{<br>    ip_hash;<br>    server 192.168.0.1;<br>    server 192.168.0.2;<br>}</p>
<p>　　4. fair（第三方）：需要安装Upstream Fair Balancer Module，按后端服务器的响应时间来分配请求，响应时间短的优先分配。<br>upstream resinserver{<br>    server server1;<br>    server server2;<br>    fair;<br>}<br>　　5. url_hash（第三方）按访问url的hash结果来分配请求，使每个url定向到同一个后端服务器，后端服务器为缓存时比较有效。<br>例：在upstream中加入hash语句，server语句中不能写入weight等其他的参数，hash_method是使用的hash算法</p>
<p>upstream resinserver{<br>    server squid1:3128;<br>    server squid2:3128;<br>    hash $request_uri;<br>    hash_method crc32;<br>}</p>
<h2 id="u53C2_u6570_u914D_u7F6E"><a href="#u53C2_u6570_u914D_u7F6E" class="headerlink" title="参数配置"></a>参数配置</h2><p>　　我们可以在upstream模块中，为每个服务设置不同的参数，如下所示：<br>upstream resinserver{ #定义负载均衡设备的Ip及设备状态<br>    ip_hash;<br>    server 127.0.0.1:8000 down;<br>    server 127.0.0.1:8080 weight=2;<br>    server 127.0.0.1:6801;<br>    server 127.0.0.1:6802 backup;<br>}<br>　　每个设备的状态设置为:<br>　　1. down  表示当前的server暂时不参与负载<br>　　2. weight  默认为1。weight越大，负载的权重就越大。<br>　　3. max_fails  允许请求失败的次数默认为1.当超过最大次数时，返回proxy_next_upstream 模块定义的错误<br>　　4. fail_timeout  max_fails次失败后，暂停的时间。<br>　　5. backup  其它所有的非backup机器down或者忙的时候，请求backup机器。所以这台机器压力会最轻。</p>
<p>nginx支持同时设置多组的负载均衡，用来给不用的server来使用。<br>client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug<br>client_body_temp_path 设置记录文件的目录可以设置最多3层目录<br>location 对URL进行匹配，可以进行重定向或者进行新的代理负载均衡</p>
<h2 id="Nginx_u914D_u7F6E"><a href="#Nginx_u914D_u7F6E" class="headerlink" title="Nginx配置"></a>Nginx配置</h2><p>　　nginx的反向代理主要由upstream模块负责，实例配置如下<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#使用的用户和组</span></span><br><span class="line"><span class="comment">#user nobody;</span></span><br><span class="line"><span class="comment">#指定工作衍生进程数，一般等于工作进程的总核数或者总核数的两倍</span></span><br><span class="line">worker_processes <span class="number">1</span>;</span><br><span class="line"><span class="comment">#pid logs/nginx.pid;</span></span><br><span class="line"><span class="comment">#指定错误日志存放的路径和级别（debug info notice warn error crit）</span></span><br><span class="line">error_<span class="built_in">log</span> /data/logs/nginx_error.log crit</span><br><span class="line">events &#123;</span><br><span class="line">　　<span class="comment">#使用的网络I/O模型，Linux系统推荐采用epoll模型</span></span><br><span class="line">　　use epoll;</span><br><span class="line">　　<span class="comment">#允许的连接数</span></span><br><span class="line">    worker_connections <span class="number">1024</span>;</span><br><span class="line">&#125;</span><br><span class="line">http &#123;</span><br><span class="line">    include mime.types;</span><br><span class="line">    default_<span class="built_in">type</span> application/octet-stream;</span><br><span class="line">    sendfile on;</span><br><span class="line">    keepalive_timeout <span class="number">65</span>;</span><br><span class="line">    <span class="comment">#gzip on;</span></span><br><span class="line">	<span class="comment">#客户端能够上传到文件大小</span></span><br><span class="line">    client_max_body_size <span class="number">8</span>m;</span><br><span class="line">    upstream cluster &#123;</span><br><span class="line">        <span class="comment">#server 192.98.12.60:1985 weight=3 max_fails=2 fail_timeout=30s;</span></span><br><span class="line">        server <span class="number">192.98</span>.<span class="number">12.42</span>:<span class="number">8086</span> weight=<span class="number">3</span> max_fails=<span class="number">2</span> fail_timeout=<span class="number">30</span>s;</span><br><span class="line">    &#125;</span><br><span class="line">    server &#123;</span><br><span class="line">        listen <span class="number">8090</span>;</span><br><span class="line">        server_name localhost;</span><br><span class="line">        <span class="comment">#charset koi8-r;</span></span><br><span class="line">        <span class="comment">#access_log logs/host.access.log main;</span></span><br><span class="line"></span><br><span class="line">        location / &#123;</span><br><span class="line">            root html;</span><br><span class="line">            index index.html index.htm;</span><br><span class="line">        &#125;</span><br><span class="line">        location /tileservice &#123; //<span class="comment">#url:"http://localhost:8090/tileservice?type=&#123;type&#125;&amp;x=&#123;x&#125;&amp;y=&#123;y&#125;&amp;l=&#123;l&#125;"</span></span><br><span class="line">            proxy_pass http://cluster/MongoTileService/tileService;</span><br><span class="line">            proxy_<span class="built_in">set</span>_header Host <span class="variable">$host</span>;</span><br><span class="line">            proxy_<span class="built_in">set</span>_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        location ~ .*\.(apk|json)$ &#123; //<span class="comment">#http://192.98.12.67:8090/version.json</span></span><br><span class="line">            root android;</span><br><span class="line">            <span class="comment">#expires 30d;</span></span><br><span class="line">            index version.json; </span><br><span class="line">        &#125;</span><br><span class="line">        location /baidu &#123; </span><br><span class="line">            proxy_pass https://www.google.com.hk;</span><br><span class="line">            proxy_<span class="built_in">set</span>_header Host <span class="variable">$host</span>;</span><br><span class="line">            proxy_<span class="built_in">set</span>_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="u52A8_u9759_u5206_u79BB"><a href="#u52A8_u9759_u5206_u79BB" class="headerlink" title="动静分离"></a>动静分离</h2><p>　　有些时候，我们希望将共用的静态资源（比如：jquery、css），直接放在nginx下。背后的web app server，只处理动态资源(比如：.aspx/.do/.jsp)，可以改进一下：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">	listen       <span class="number">80</span>;</span><br><span class="line">	server_name  www.aaa.com;</span><br><span class="line">	root R:/www/aaa.com;</span><br><span class="line">	location / &#123;</span><br><span class="line">		proxy_pass http://<span class="number">192.168</span>.<span class="number">1.201</span>:<span class="number">8080</span> ;</span><br><span class="line">		proxy_<span class="built_in">set</span>_header Host <span class="variable">$host</span>:<span class="number">8080</span>;</span><br><span class="line">		proxy_<span class="built_in">set</span>_header X-Real-IP <span class="variable">$remote_addr</span>;</span><br><span class="line">		proxy_<span class="built_in">set</span>_header X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">		proxy_<span class="built_in">set</span>_header Via <span class="string">"nginx"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	location ~ .*\.(js|css)?$ &#123;</span><br><span class="line">		expires <span class="number">1</span>h;</span><br><span class="line">	&#125;</span><br><span class="line">	access_<span class="built_in">log</span> off;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　这样，访问<a href="http://www.aaa.com/content/site.css" target="_blank" rel="external">http://www.aaa.com/content/site.css</a> 时实际上读取的是R:/www/aaa.com/content/site.css 这个文件</p>
<h2 id="u865A_u62DF_u4E3B_u673A"><a href="#u865A_u62DF_u4E3B_u673A" class="headerlink" title="虚拟主机"></a>虚拟主机</h2><p>　　这里配置了二个虚拟主机，分别绑定到域名www.aaa.com及www.bbb.com，详见资源<a href="/code/第3章 Nginx的基本配置与优化.pdf">Nginx的基本配置与优化.pdf</a><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">	listen       <span class="number">80</span>;</span><br><span class="line">	server_name  www.aaa.com;</span><br><span class="line">	access_<span class="built_in">log</span> logs/aaa.access.log combined;</span><br><span class="line">	location / &#123;</span><br><span class="line">		root   r:/www/aaa.com;</span><br><span class="line">		index  index.html index.htm;</span><br><span class="line">	&#125;       </span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">	listen       <span class="number">80</span>;</span><br><span class="line">	server_name  www.bbb.com;</span><br><span class="line">	access_<span class="built_in">log</span> logs/bbb.access.log combined;</span><br><span class="line">	location / &#123;</span><br><span class="line">		root   r:/www/bbb.com;</span><br><span class="line">		index  index.html index.htm;</span><br><span class="line">	&#125;       </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　我们再看另一个配置，该配置主要实现的功能如下：</p>
<ol>
<li>用户访问<a href="http://www.zyan.cc，将其负载均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:80四台服务器。" target="_blank" rel="external">http://www.zyan.cc，将其负载均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:80四台服务器。</a></li>
<li>用户访问<a href="http://blog.zyan.cc，将其负载均衡到192.168.1.7服务器的8080、8081、8082端口。" target="_blank" rel="external">http://blog.zyan.cc，将其负载均衡到192.168.1.7服务器的8080、8081、8082端口。</a></li>
</ol>
<p>nginx.conf配置文件如下：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">user  www www;</span><br><span class="line"></span><br><span class="line">worker_processes <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">#error_log  logs/error.log;</span></span><br><span class="line"><span class="comment">#error_log  logs/error.log  notice;</span></span><br><span class="line"><span class="comment">#error_log  logs/error.log  info;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#pid        logs/nginx.pid;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#最大文件描述符</span></span><br><span class="line">worker_rlimit_nofile <span class="number">51200</span>;</span><br><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">      use epoll;</span><br><span class="line"></span><br><span class="line">      worker_connections <span class="number">51200</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http &#123;</span><br><span class="line">      include       conf/mime.types;</span><br><span class="line">      default_<span class="built_in">type</span>  application/octet-stream;</span><br><span class="line"></span><br><span class="line">      keepalive_timeout <span class="number">120</span>;</span><br><span class="line"></span><br><span class="line">      tcp_nodelay on;</span><br><span class="line"></span><br><span class="line">      upstream  www.zyan.cc  &#123;</span><br><span class="line">              server   <span class="number">192.168</span>.<span class="number">1.2</span>:<span class="number">80</span>;</span><br><span class="line">              server   <span class="number">192.168</span>.<span class="number">1.3</span>:<span class="number">80</span>;</span><br><span class="line">              server   <span class="number">192.168</span>.<span class="number">1.4</span>:<span class="number">80</span>;</span><br><span class="line">              server   <span class="number">192.168</span>.<span class="number">1.5</span>:<span class="number">80</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      upstream  blog.zyan.cc  &#123;</span><br><span class="line">              server   <span class="number">192.168</span>.<span class="number">1.7</span>:<span class="number">8080</span>;</span><br><span class="line">              server   <span class="number">192.168</span>.<span class="number">1.7</span>:<span class="number">8081</span>;</span><br><span class="line">              server   <span class="number">192.168</span>.<span class="number">1.7</span>:<span class="number">8082</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      server&#123;</span><br><span class="line">              listen  <span class="number">80</span>;</span><br><span class="line">              server_name  www.zyan.cc;</span><br><span class="line"></span><br><span class="line">              location / &#123;</span><br><span class="line">                       proxy_pass        http://www.zyan.cc;</span><br><span class="line">                       proxy_<span class="built_in">set</span>_header   Host             <span class="variable">$host</span>;</span><br><span class="line">                       proxy_<span class="built_in">set</span>_header   X-Real-IP        <span class="variable">$remote_addr</span>;</span><br><span class="line">                       proxy_<span class="built_in">set</span>_header   X-Forwarded-For  <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">              &#125;</span><br><span class="line"></span><br><span class="line">              <span class="built_in">log</span>_format  www_s135_com  <span class="string">'$remote_addr - $remote_user [$time_local] $request '</span></span><br><span class="line">                                <span class="string">'"$status" $body_bytes_sent "$http_referer" '</span></span><br><span class="line">                                <span class="string">'"$http_user_agent" "$http_x_forwarded_for"'</span>;</span><br><span class="line">              access_<span class="built_in">log</span>  /data1/logs/www.log  www_s135_com;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      server&#123;</span><br><span class="line">              listen  <span class="number">80</span>;</span><br><span class="line">              server_name  blog.zyan.cc;</span><br><span class="line"></span><br><span class="line">              location / &#123;</span><br><span class="line">                       proxy_pass        http://blog.zyan.cc;</span><br><span class="line">                       proxy_<span class="built_in">set</span>_header   Host             <span class="variable">$host</span>;</span><br><span class="line">                       proxy_<span class="built_in">set</span>_header   X-Real-IP        <span class="variable">$remote_addr</span>;</span><br><span class="line">                       proxy_<span class="built_in">set</span>_header   X-Forwarded-For  <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">              &#125;</span><br><span class="line"></span><br><span class="line">              <span class="built_in">log</span>_format  blog_s135_com  <span class="string">'$remote_addr - $remote_user [$time_local] $request '</span></span><br><span class="line">                                <span class="string">'"$status" $body_bytes_sent "$http_referer" '</span></span><br><span class="line">                                <span class="string">'"$http_user_agent" "$http_x_forwarded_for"'</span>;</span><br><span class="line">              access_<span class="built_in">log</span>  /data1/logs/blog.log  blog_s135_com;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/17/nginx/2.Nginx系列之反向代理/" data-id="cijzzpv8a006r1cfslz0qv2yn" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/nginx/">nginx</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-nginx/1.Nginx系列之简介与配置" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/17/nginx/1.Nginx系列之简介与配置/" class="article-date">
  <time datetime="2015-12-16T17:00:00.000Z" itemprop="datePublished">2015-12-17</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/nginx/">nginx</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/17/nginx/1.Nginx系列之简介与配置/">Nginx系列一之简介与配置</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u5B89_u88C5"><a href="#u5B89_u88C5" class="headerlink" title="安装"></a>安装</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">tar zxvf nginx-x.xxx.xx.tar.gz</span><br><span class="line"><span class="built_in">cd</span> nginx-x.xxx.xx</span><br><span class="line">./configure</span><br><span class="line">make</span><br><span class="line">make install</span><br></pre></td></tr></table></figure>
<p>　　按照以上命令，Nginx将被默认安装到/usr/local/nginx目录下，可以通过./configure –help命令查看Nginx可选择的编译选项，如下所示：</p>
<ol>
<li>–prefix=<path></path> Nginx安装路径。如果没指定，默认为/usr/local/nginx</li>
<li>–sbin-path=<path></path> Nginx可执行文件安装路径。只能安装时指定，默认为<prefix>/sbin/nginx</prefix></li>
<li>–conf-path=<path></path> 默认的nginx.conf路径，nginx在启动时，可以使用参数-c指定配置nginx.conf的路径。如下：/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf</li>
</ol>
<h2 id="u5E38_u7528_u547D_u4EE4"><a href="#u5E38_u7528_u547D_u4EE4" class="headerlink" title="常用命令"></a>常用命令</h2><p>nginx -s [stop | quit | reopen | reload]</p>
<p>../nginx/sbin/nginx -t 检查配置文件是否正确</p>
<p>　　启动完成后通过 ps -ef | grep nginx 可以看到，默认nginx有一个master process主进程，另外四个worker process。同时nginx.pid文件默认存放在Nginx安装目录的logs目录下</p>
<p>　　Nginx支持平滑重启（reload），平滑升级</p>
<h2 id="u81EA_u52A8_u5217_u76EE_u5F55_u914D_u7F6E"><a href="#u81EA_u52A8_u5217_u76EE_u5F55_u914D_u7F6E" class="headerlink" title="自动列目录配置"></a>自动列目录配置</h2><p>　　一些开源软件的下载页面是能够自动列目录的。这一功能apache可以实现，Nginx同样可以实现。前提条件是当前目录下不存在用index指令设置的默认首页文件。如果需要在某一虚拟主机的location / {…}目录控制自动列目录，只需加上如下代码<br>location / {<br>    autoindex on;<br>}</p>
<h2 id="Rewrite_u89C4_u5219_u7F16_u5199_u5B9E_u4F8B"><a href="#Rewrite_u89C4_u5219_u7F16_u5199_u5B9E_u4F8B" class="headerlink" title="Rewrite规则编写实例"></a>Rewrite规则编写实例</h2><p>　　文件和目录不存在时，重定向到某个文件上，详见资源：<a href="/code/第7章 Nginx的Rewrite规则与实例.pdf">Nginx的Rewrite规则与实例.pdf</a></p>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://blog.sina.com.cn/s/articlelist_1834459124_0_1.html" target="_blank" rel="external">http://blog.sina.com.cn/s/articlelist_1834459124_0_1.html</a><br>张宴博客<br>OpenResty</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/17/nginx/1.Nginx系列之简介与配置/" data-id="cijzzpv8d006u1cfsxwgyhc1r" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/nginx/">nginx</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-git/2.git系列之问题汇总" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/16/git/2.git系列之问题汇总/" class="article-date">
  <time datetime="2015-12-15T18:00:00.000Z" itemprop="datePublished">2015-12-16</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/git/">git</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/16/git/2.git系列之问题汇总/">git系列二之问题汇总</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u5B89_u88C5"><a href="#u5B89_u88C5" class="headerlink" title="安装"></a>安装</h2><p>　　1. 下载安装msysgit<br>　　2. 配置git账号<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global user.name <span class="string">"Your Name"</span></span><br><span class="line">$ git config --global user.email <span class="string">"email@example.com"</span></span><br></pre></td></tr></table></figure></p>
<h2 id="TortoiseGit"><a href="#TortoiseGit" class="headerlink" title="TortoiseGit"></a>TortoiseGit</h2><p>　　TortoiseGit 连接github时每次都需要输入用户名和密码，很是麻烦，下面我教大家怎么处理这个问题。当我们安装好配置好git后，在C:\Users\xxxxx目录下有一个.gitconfig的文件，里面会有你先前配好的name 和email，只需在下面加一行：</p>
<pre><code class="bash">[credential]      
    helper = store
</code></pre>
<p>　　下次再输入用户名和密码时，git就会记住，从而在C:\Users\xxxxx目录下形成一个.git-credentials 文件，里面就是保存的你的用户名和密码。</p>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000" target="_blank" rel="external">http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/16/git/2.git系列之问题汇总/" data-id="cijzzpvff00eu1cfsb63r3xyu" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/git/">git</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-git/1.git系列之多账号配置" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/16/git/1.git系列之多账号配置/" class="article-date">
  <time datetime="2015-12-15T17:00:00.000Z" itemprop="datePublished">2015-12-16</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/git/">git</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/16/git/1.git系列之多账号配置/">git系列一之多账号配置</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u7B80_u4ECB"><a href="#u7B80_u4ECB" class="headerlink" title="简介"></a>简介</h2><p>　　github则作为全球最大的开源社区，作为一名有志向的攻城狮，注册一个账号是有必要使用的。同时，公司的项目管理也是采用git。这样就出现了一个问题，如何在一台电脑上同时配置多个git账号呢！根据网上各种资料，归纳整理，剔除不必要的步骤，最终形成了下面的操作手册。</p>
<h2 id="u914D_u7F6E_u8FC7_u7A0B"><a href="#u914D_u7F6E_u8FC7_u7A0B" class="headerlink" title="配置过程"></a>配置过程</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 先进入D:\Users\shihc\.ssh目录</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置第一个账号</span></span><br><span class="line">ssh-keygen -t rsa -C <span class="string">"第一个邮箱地址"</span></span><br><span class="line"><span class="comment"># 回车提示输入key的保存文件，输入：id_rsa_github</span></span><br><span class="line"><span class="comment"># 提示输入密码，直接回车即可</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 因为默认只读取id_rsa，为了让SSH识别新的私钥，需将其添加到SSH agent中：</span></span><br><span class="line">ssh-add ~/.ssh/id_rsa_github</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果出现错误Could not open a connection to your authentication agent.，则输入：</span></span><br><span class="line">ssh-agent bash</span><br><span class="line"><span class="comment"># 再执行</span></span><br><span class="line">ssh-add ~/.ssh/id_rsa_github</span><br><span class="line"></span><br><span class="line"><span class="comment"># 同理再次执行上面的步骤，配置第二个账号</span></span><br><span class="line"><span class="comment"># 配置第二个账号</span></span><br><span class="line">ssh-keygen -t rsa -C <span class="string">"第二个邮箱地址"</span></span><br><span class="line"><span class="comment"># 回车提示输入key的保存文件，输入：id_rsa_work</span></span><br><span class="line"><span class="comment"># 提示输入密码，直接回车即可</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 因为默认只读取id_rsa，为了让SSH识别新的私钥，需将其添加到SSH agent中：</span></span><br><span class="line">ssh-add ~/.ssh/id_rsa_work</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果出现错误Could not open a connection to your authentication agent.，则输入：</span></span><br><span class="line">ssh-agent bash</span><br><span class="line"><span class="comment"># 再执行</span></span><br><span class="line">ssh-add ~/.ssh/id_rsa_work</span><br></pre></td></tr></table></figure>
<p>　　至于需要将新生成的~/.ssh/id_rsa_work.pub文件，将里面的内容添加到GitHub后台的步骤，好像不需要，也可能是之前已经添加过的原因吧！</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/16/git/1.git系列之多账号配置/" data-id="cijzzpvfj00ez1cfsudgamoyp" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/git/">git</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-log/11.日志系列之log4j 2" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/15/log/11.日志系列之log4j 2/" class="article-date">
  <time datetime="2015-12-15T03:00:00.000Z" itemprop="datePublished">2015-12-15</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/log/">log</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/15/log/11.日志系列之log4j 2/">日志系列十一之log4j 2</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u6846_u67B6_u642D_u914D"><a href="#u6846_u67B6_u642D_u914D" class="headerlink" title="框架搭配"></a>框架搭配</h2><p>　　在之前一系列的日志框架中，我们主要介绍了各个日志框架的使用方法，并对源码进行了简单的分析。本篇文章着重介绍各日志框架的缺点，如下所示。</p>
<ol>
<li>日志接口：主要包括commons-logging、slf4j-api包，他们基本上没有任何实现。仅仅是提供一个一套标准方法，为提供日志输出的能力。</li>
<li>日志实现：主要包括log4j、lagback。<br>　　为了在各个系统中能够切换不同的日志框架，现在流行的搭配基本上就是：slf4j-api(标准) + slf4j-log4j12(桥接包) + log4j(日志实现)，或者是slf4j-api(标准) + logback(内部实现了与slf4j的桥接功能和日志实现功能)。<br>　　但是在实际的使用中，我们经常还会看到很多与日志相关的其他包，下面我们逐一进行分析：<h2 id="jcl-over-slf4j"><a href="#jcl-over-slf4j" class="headerlink" title="jcl-over-slf4j"></a>jcl-over-slf4j</h2>　　jcl-over-slf4j用于替换commons-logging，并且自动依赖slf4j-api包，使采用commons-logging框架的包无缝切换到slf4j。比如spring的一些核心包就是采用的commons-logging。此时怎么做呢，如下所示，在pom.xml中进行配置即可<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="title">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">artifactId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="title">artifactId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">version</span>&gt;</span>3.0.0.RELEASE<span class="tag">&lt;/<span class="title">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="title">scope</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">exclusions</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="title">exclusion</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="title">groupId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="title">groupId</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="title">artifactId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="title">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="title">exclusion</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="title">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="title">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">artifactId</span>&gt;</span>jcl-over-slf4j<span class="tag">&lt;/<span class="title">artifactId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">version</span>&gt;</span>1.5.8<span class="tag">&lt;/<span class="title">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="title">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>　　这是什么实现原理呢？我们展开jcl-over-slf4j包，如下图所示。<br><img src="/imagesj/cl-over-slf4j.jpg" alt="jcl-over-slf4j.jpg"><br>　　可以看到，其基本上完全重写commons-logging，但是新增了SLF4JLogFactory。同时作者修改了LogFactory的实现，使其采用SLF4JLogFactory的实现，关键代码如下：<br>public abstract class LogFactory {<br>  static LogFactory logFactory = new SLF4JLogFactory();</p>
<p>  public static LogFactory getFactory() throws LogConfigurationException {<br>    return logFactory;<br>  }<br>  public static Log getLog(Class clazz) throws LogConfigurationException {<br>    return (getFactory().getInstance(clazz));<br>  }<br>}<br>　　很简单，是吧！！</p>
<h2 id="log4j-over-slf4j"><a href="#log4j-over-slf4j" class="headerlink" title="log4j-over-slf4j"></a>log4j-over-slf4j</h2><p>　　与jcl-over-slf4j类似，其替换log4j，同样也添加了对slf4j-api包的依赖，原理类似，这里不再介绍</p>
<h2 id="u4F18_u7F3A_u70B9"><a href="#u4F18_u7F3A_u70B9" class="headerlink" title="优缺点"></a>优缺点</h2><p>　　log4j：为了提高运行效率，往往在输出信息之前，还要进行级别判断，以避免无效的字符串连接操作。if (logger.isDebugEnabled()) logger.debug(“debug：“+name);<br>　　slf4j：先传入带有占位符的字符串，同时把其他参数传入，在slf4j的内容部实现中，如果级别合适再用传入的参数去替换字符串中的占位符，否则不用执行。logger.info(“{} is {}”, new String[]{“x”,“y”});</p>
<h2 id="Log4j_u6027_u80FD_u4F18_u5316"><a href="#Log4j_u6027_u80FD_u4F18_u5316" class="headerlink" title="Log4j性能优化"></a>Log4j性能优化</h2><p>　　Log4j一直被人所诟病的也就是它的性能问题了，以下log4j的注意事项主要是网上摘录的，只做参考。<br>　　1. 如果你的日志实例的名称不是经常变化，请将它定义为static变量。<br>　　2. 如果你的message构造很复杂，那么在构造之前，请先使用isXXXEnabled（）判断。<br>　　3. 配置文件中格式控制如果不需要，尽量不要输出类名和行号。<br>　　4. 根据log4j的级别判断控制流程，在配置文件中尽可能配置的比较细致，Logger.info(message)的比较流程：<br>　　　　a. 比较日志仓库的threashold<br>　　　　b. 与自身的有效Level（没有则是父亲的）比较<br>　　　　c. 与Appender的threashold比较<br>　　　　d. 询问Appender的filter<br>　　　　e. Layout格式化<br>　　　　f. 记录日志<br>　　5. 如果Appender是文件类型，请不要把文件大小设的太小。至少设为10MB（<param name="MaxFileSize" value="10MB">）。<br>　　6. 如果不是即时调试程序，把你的级别设定为高级别，最好是threshold=INFO之上。<br>　　7. 如果不是想即时看到日志信息，你也可以把Appender的ImmediateFlush 设为false（<param name="ImmediateFlush" value="false">）。<br>　　8. 如果采用DBAppender的模式，log4j默认采用的是短连接，建议改成数据库连接池的方式<br>　　9. 设置日志缓存，以及缓存大小<br>        log4j.appender.A3.BufferedIO=true   </p>
<pre><code>    #Buffer单位为字节，默认是8K，IO BLOCK大小默认也是8K    
    log4j.appender.A3.BufferSize=8192 
9. 设置日志输出为异步方式，**AsyncAppender**采用的是生产者消费者的模型进行异步地将Logging Event送到对应的Appender中。
10. 当日志量很大时，配置log4j.appender.monitorAppender.DatePattern=&apos;.&apos;yyyy-MM-dd-HH，这个选项用于告诉DailyRollingFileAppender每小时输出日志，而不是默认的一天输出一次
</code></pre><h2 id="logback_u4F18_u52BF"><a href="#logback_u4F18_u52BF" class="headerlink" title="logback优势"></a>logback优势</h2><p>　　相比于log4j，logback作为优化的版本，其优化点主要体现在如下几点：<br>　　1. 支持配置文件周期性的扫描，实时感知配置文件的变化。当然log4j也可以借助于spring的支持实现该功能<br>　　2. 自动去除旧的日志文件：通过设置TimeBasedRollingPolicy或者SizeAndTimeBasedFNATP的maxHistory属性，你可以控制已经产生日志文件的最大数量。如果设置maxHistory为12，那那些log文件超过12个月的都会被自动移除。<br>　　3. SiftingAppender(一个非常多功能的Appender)：它可以用来分割日志文件根据任何一个给定的运行参数。如，SiftingAppender能够区别日志事件跟进用户的Session，然后每个用户会有一个日志文件。<br>　　4. Logback-access模块：作为logback发布包的一部分，logback-access模块可与Jetty或者Tomcat进行集成，提供了非常丰富而强大的通过HTTP访问日志的功能。因为logback-access模块是logback初期设计方案中的一部分，因此，所有你所喜欢的logback-classic模块所提供的全部特性logback-access同样也具备。<br>　　5. 自动压缩归档日志文件：RollingFileAppender可以在回滚操作中，自动压缩归档日志文件。压缩通常是异步执行的，所以即使是很大的日志文件，你的应用都不会因此而被阻塞。<br>　　6.优雅地从I/O错误中恢复：FileAppender和它的子类，包括RollingFileAppender，可以优雅的从I/O错误中恢复。所以，如果一个文件服务器临时宕机，你再也不需要重启你的应用，而日志功能就能正常工作。当文件服务器恢复工作，logback相关的appender就会透明地和快速的从上一个错误中恢复。</p>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://www.open-open.com/lib/view/open1431097581148.html" target="_blank" rel="external">http://www.open-open.com/lib/view/open1431097581148.html</a><br><a href="http://www.oschina.net/translate/reasons-to-prefer-logbak-over-log4j" target="_blank" rel="external">http://www.oschina.net/translate/reasons-to-prefer-logbak-over-log4j</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/15/log/11.日志系列之log4j 2/" data-id="cijzzpvc900b51cfs5dvlk1py" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/log/">log</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/log4j/">log4j</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-log/10.日志系列之框架比较" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/15/log/10.日志系列之框架比较/" class="article-date">
  <time datetime="2015-12-15T02:00:00.000Z" itemprop="datePublished">2015-12-15</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/log/">log</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/15/log/10.日志系列之框架比较/">日志系列六之框架比较</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u6846_u67B6_u642D_u914D"><a href="#u6846_u67B6_u642D_u914D" class="headerlink" title="框架搭配"></a>框架搭配</h2><p>　　在之前一系列的日志框架中，我们主要介绍了各个日志框架的使用方法，并对源码进行了简单的分析。本篇文章着重介绍各日志框架的缺点，如下所示。</p>
<ol>
<li>日志接口：主要包括commons-logging、slf4j-api包，他们基本上没有任何实现。仅仅是提供一个一套标准方法，为提供日志输出的能力。</li>
<li>日志实现：主要包括log4j、lagback。<br>　　为了在各个系统中能够切换不同的日志框架，现在流行的搭配基本上就是：slf4j-api(标准) + slf4j-log4j12(桥接包) + log4j(日志实现)，或者是slf4j-api(标准) + logback(内部实现了与slf4j的桥接功能和日志实现功能)。<br>　　但是在实际的使用中，我们经常还会看到很多与日志相关的其他包，下面我们逐一进行分析：<h2 id="jcl-over-slf4j"><a href="#jcl-over-slf4j" class="headerlink" title="jcl-over-slf4j"></a>jcl-over-slf4j</h2>　　jcl-over-slf4j用于替换commons-logging，并且自动依赖slf4j-api包，使采用commons-logging框架的包无缝切换到slf4j。比如spring的一些核心包就是采用的commons-logging。此时怎么做呢，如下所示，在pom.xml中进行配置即可<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="title">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">artifactId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="title">artifactId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">version</span>&gt;</span>3.0.0.RELEASE<span class="tag">&lt;/<span class="title">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="title">scope</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">exclusions</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="title">exclusion</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="title">groupId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="title">groupId</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="title">artifactId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="title">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="title">exclusion</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="title">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="title">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">artifactId</span>&gt;</span>jcl-over-slf4j<span class="tag">&lt;/<span class="title">artifactId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">version</span>&gt;</span>1.5.8<span class="tag">&lt;/<span class="title">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="title">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="title">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>　　这是什么实现原理呢？我们展开jcl-over-slf4j包，如下图所示。<br><img src="/imagesj/cl-over-slf4j.jpg" alt="jcl-over-slf4j.jpg"><br>　　可以看到，其基本上完全重写commons-logging，但是新增了SLF4JLogFactory。同时作者修改了LogFactory的实现，使其采用SLF4JLogFactory的实现，关键代码如下：<br>public abstract class LogFactory {<br>  static LogFactory logFactory = new SLF4JLogFactory();</p>
<p>  public static LogFactory getFactory() throws LogConfigurationException {<br>    return logFactory;<br>  }<br>  public static Log getLog(Class clazz) throws LogConfigurationException {<br>    return (getFactory().getInstance(clazz));<br>  }<br>}<br>　　很简单，是吧！！</p>
<h2 id="log4j-over-slf4j"><a href="#log4j-over-slf4j" class="headerlink" title="log4j-over-slf4j"></a>log4j-over-slf4j</h2><p>　　与jcl-over-slf4j类似，其替换log4j，同样也添加了对slf4j-api包的依赖，原理类似，这里不再介绍</p>
<h2 id="u4F18_u7F3A_u70B9"><a href="#u4F18_u7F3A_u70B9" class="headerlink" title="优缺点"></a>优缺点</h2><p>　　log4j：为了提高运行效率，往往在输出信息之前，还要进行级别判断，以避免无效的字符串连接操作。if (logger.isDebugEnabled()) logger.debug(“debug：“+name);<br>　　slf4j：先传入带有占位符的字符串，同时把其他参数传入，在slf4j的内容部实现中，如果级别合适再用传入的参数去替换字符串中的占位符，否则不用执行。logger.info(“{} is {}”, new String[]{“x”,“y”});</p>
<h2 id="Log4j_u6027_u80FD_u4F18_u5316"><a href="#Log4j_u6027_u80FD_u4F18_u5316" class="headerlink" title="Log4j性能优化"></a>Log4j性能优化</h2><p>　　Log4j一直被人所诟病的也就是它的性能问题了，以下log4j的注意事项主要是网上摘录的，只做参考。<br>　　1. 如果你的日志实例的名称不是经常变化，请将它定义为static变量。<br>　　2. 如果你的message构造很复杂，那么在构造之前，请先使用isXXXEnabled（）判断。<br>　　3. 配置文件中格式控制如果不需要，尽量不要输出类名和行号。<br>　　4. 根据log4j的级别判断控制流程，在配置文件中尽可能配置的比较细致，Logger.info(message)的比较流程：<br>　　　　a. 比较日志仓库的threashold<br>　　　　b. 与自身的有效Level（没有则是父亲的）比较<br>　　　　c. 与Appender的threashold比较<br>　　　　d. 询问Appender的filter<br>　　　　e. Layout格式化<br>　　　　f. 记录日志<br>　　5. 如果Appender是文件类型，请不要把文件大小设的太小。至少设为10MB（<param name="MaxFileSize" value="10MB">）。<br>　　6. 如果不是即时调试程序，把你的级别设定为高级别，最好是threshold=INFO之上。<br>　　7. 如果不是想即时看到日志信息，你也可以把Appender的ImmediateFlush 设为false（<param name="ImmediateFlush" value="false">）。<br>　　8. 如果采用DBAppender的模式，log4j默认采用的是短连接，建议改成数据库连接池的方式<br>　　9. 设置日志缓存，以及缓存大小<br>        log4j.appender.A3.BufferedIO=true   </p>
<pre><code>    #Buffer单位为字节，默认是8K，IO BLOCK大小默认也是8K    
    log4j.appender.A3.BufferSize=8192 
9. 设置日志输出为异步方式，**AsyncAppender**采用的是生产者消费者的模型进行异步地将Logging Event送到对应的Appender中。
10. 当日志量很大时，配置log4j.appender.monitorAppender.DatePattern=&apos;.&apos;yyyy-MM-dd-HH，这个选项用于告诉DailyRollingFileAppender每小时输出日志，而不是默认的一天输出一次
</code></pre><h2 id="logback_u4F18_u52BF"><a href="#logback_u4F18_u52BF" class="headerlink" title="logback优势"></a>logback优势</h2><p>　　相比于log4j，logback作为优化的版本，其优化点主要体现在如下几点：<br>　　1. 支持配置文件周期性的扫描，实时感知配置文件的变化。当然log4j也可以借助于spring的支持实现该功能<br>　　2. 自动去除旧的日志文件：通过设置TimeBasedRollingPolicy或者SizeAndTimeBasedFNATP的maxHistory属性，你可以控制已经产生日志文件的最大数量。如果设置maxHistory为12，那那些log文件超过12个月的都会被自动移除。<br>　　3. SiftingAppender(一个非常多功能的Appender)：它可以用来分割日志文件根据任何一个给定的运行参数。如，SiftingAppender能够区别日志事件跟进用户的Session，然后每个用户会有一个日志文件。<br>　　4. Logback-access模块：作为logback发布包的一部分，logback-access模块可与Jetty或者Tomcat进行集成，提供了非常丰富而强大的通过HTTP访问日志的功能。因为logback-access模块是logback初期设计方案中的一部分，因此，所有你所喜欢的logback-classic模块所提供的全部特性logback-access同样也具备。<br>　　5. 自动压缩归档日志文件：RollingFileAppender可以在回滚操作中，自动压缩归档日志文件。压缩通常是异步执行的，所以即使是很大的日志文件，你的应用都不会因此而被阻塞。<br>　　6.优雅地从I/O错误中恢复：FileAppender和它的子类，包括RollingFileAppender，可以优雅的从I/O错误中恢复。所以，如果一个文件服务器临时宕机，你再也不需要重启你的应用，而日志功能就能正常工作。当文件服务器恢复工作，logback相关的appender就会透明地和快速的从上一个错误中恢复。</p>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://www.open-open.com/lib/view/open1431097581148.html" target="_blank" rel="external">http://www.open-open.com/lib/view/open1431097581148.html</a><br><a href="http://www.oschina.net/translate/reasons-to-prefer-logbak-over-log4j" target="_blank" rel="external">http://www.oschina.net/translate/reasons-to-prefer-logbak-over-log4j</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/15/log/10.日志系列之框架比较/" data-id="cijzzpvcd00b91cfsvcm69nwo" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/log/">log</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/log4j/">log4j</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-log/9.logback系列之Appender" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/15/log/9.logback系列之Appender/" class="article-date">
  <time datetime="2015-12-15T01:00:00.000Z" itemprop="datePublished">2015-12-15</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/log/">log</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/15/log/9.logback系列之Appender/">logback系列五之Appender</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="UML"><a href="#UML" class="headerlink" title="UML"></a>UML</h2><p>　　实际上Appender可能是logback框架中最重要的组件之一，虽然Logger是记录日志的接口，但是如果一个Logger没有关联到任何Appender的话，那么这个Logger就无法记录任何信息。此外虽然logback提供了很多扩展点，但是在应用中，我们可能很少会扩展filter，很少扩展layout和encoder，但是我们扩展Appender的机会却是很多的。下面我们通过两张uml图，看看appender的继承关系<br><img src="/images/socketAppender.png" alt="socketAppender.png"><br><img src="/images/unsynchronizedAppender.png" alt="unsynchronizedAppender.png"><br>　　从上面的两张图可以看出，Appender接口有2个实现类，，一个是AppenderBase，另一个是UnsynchronizedAppenderBase。OutputStreamAppender是继承自UnsynchronizedAppenderBase的Appender实现类，虽然它已经不是抽象类了，但是实际也是不能直接使用的，它的实现类就是最常见的ConsoleAppender和FileAppender。<br>　　我们进入OutputStreamAppender类的，这里只列出了几个关键的地方。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OutputStreamAppender</span>&lt;<span class="title">E</span>&gt; <span class="keyword">extends</span> <span class="title">UnsynchronizedAppenderBase</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">protected</span> Encoder&lt;E&gt; encoder;</span><br><span class="line">  <span class="keyword">protected</span> <span class="keyword">final</span> ReentrantLock lock = <span class="keyword">new</span> ReentrantLock(<span class="keyword">true</span>);</span><br><span class="line">  <span class="keyword">private</span> OutputStream outputStream;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">subAppend</span><span class="params">(E event)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!isStarted()) &#123;</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      ......</span><br><span class="line">      lock.lock();</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">        writeOut(event);</span><br><span class="line">      &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">        lock.unlock();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException ioe) &#123;</span><br><span class="line">      <span class="keyword">this</span>.started = <span class="keyword">false</span>;</span><br><span class="line">      addStatus(<span class="keyword">new</span> ErrorStatus(<span class="string">"IO failure in appender"</span>, <span class="keyword">this</span>, ioe));</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">writeOut</span><span class="params">(E event)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.encoder.doEncode(event);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　我们可以看到Appender组件还不是最终实际记录日志信息的组件，它是委托encoder组件来完成LoggingEvent的格式化和记录的。我们看看<a href="/">logback系列一之入门</a>文章中的appender节点的配置，可以发现其存在子节点encoder。</p>
<h2 id="u81EA_u5B9A_u4E49Appender"><a href="#u81EA_u5B9A_u4E49Appender" class="headerlink" title="自定义Appender"></a>自定义Appender</h2><p>　　其实，我们可以非常简单的实现一个自定义的Appender，如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DemoAppender</span> <span class="keyword">extends</span> <span class="title">AppenderBase</span>&lt;<span class="title">LoggingEvent</span>&gt; </span>&#123;  </span><br><span class="line">    <span class="annotation">@Override</span>  </span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">append</span><span class="params">(LoggingEvent eventObject)</span> </span>&#123;  </span><br><span class="line">        System.out.println(eventObject.getMessage());  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="Filter"><a href="#Filter" class="headerlink" title="Filter"></a>Filter</h2><p>　　到这里我们基本上也就完成了对logback的源码分析。实际上，还缺少Filter、Endcoder介绍。简单说下，Filter的功能类似于Servlet中的过滤器，或者是SpringMVC中的拦截器功能。下面列出两个与其相关的类的关键代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Filter</span>&lt;<span class="title">E</span>&gt; <span class="keyword">extends</span> <span class="title">ContextAwareBase</span> <span class="keyword">implements</span> <span class="title">LifeCycle</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> FilterReply <span class="title">decide</span><span class="params">(E event)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">final</span> <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FilterAttachableImpl</span>&lt;<span class="title">E</span>&gt; <span class="keyword">implements</span> <span class="title">FilterAttachable</span>&lt;<span class="title">E</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">  CopyOnWriteArrayList&lt;Filter&lt;E&gt;&gt; filterList = <span class="keyword">new</span> CopyOnWriteArrayList&lt;Filter&lt;E&gt;&gt;();</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addFilter</span><span class="params">(Filter&lt;E&gt; newFilter)</span> </span>&#123;</span><br><span class="line">    filterList.add(newFilter);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clearAllFilters</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    filterList.clear();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> FilterReply <span class="title">getFilterChainDecision</span><span class="params">(E event)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (Filter&lt;E&gt; f : filterList) &#123;</span><br><span class="line">      <span class="keyword">final</span> FilterReply r = f.decide(event);</span><br><span class="line">      <span class="keyword">if</span> (r == FilterReply.DENY || r == FilterReply.ACCEPT) &#123;</span><br><span class="line">        <span class="keyword">return</span> r;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> FilterReply.NEUTRAL;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　在AppenderBase和UnsynchronizedAppenderBase类中，我们可以看到，其在调用append()方法的时候，会先调用getFilterChainDecision方法，关键代码如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> FilterAttachableImpl&lt;E&gt; fai = <span class="keyword">new</span> FilterAttachableImpl&lt;E&gt;();</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doAppend</span><span class="params">(E eventObject)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (getFilterChainDecision(eventObject) == FilterReply.DENY) &#123;</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">this</span>.append(eventObject);</span><br><span class="line">  </span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> FilterReply <span class="title">getFilterChainDecision</span><span class="params">(E event)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> fai.getFilterChainDecision(event);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="u603B_u7ED3"><a href="#u603B_u7ED3" class="headerlink" title="总结"></a>总结</h2><p>　　到这里logback基本上就介绍完了，下一篇文章将会主要介绍各个日志框架的优缺点，方便大家在项目中的技术选型。</p>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://kyfxbl.iteye.com/blog/1173788" target="_blank" rel="external">http://kyfxbl.iteye.com/blog/1173788</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/15/log/9.logback系列之Appender/" data-id="cijzzpvbf00a41cfsdd2vdx6q" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/log/">log</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/logback/">logback</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-log/8.logback系列之debug info等" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/15/log/8.logback系列之debug info等/" class="article-date">
  <time datetime="2015-12-15T00:00:00.000Z" itemprop="datePublished">2015-12-15</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/log/">log</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/15/log/8.logback系列之debug info等/">logback系列四之debug info等</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="Logger_u6267_u884C"><a href="#Logger_u6267_u884C" class="headerlink" title="Logger执行"></a>Logger执行</h2><p>　　slf4j定义了Logger接口记录日志的方法是info()、warn()、debug()等，这些方法只是入口，而logback则对这些方法进行了实现，下面以最常用的info方法为实例进行介绍。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">info</span><span class="params">(String msg)</span> </span>&#123;</span><br><span class="line">  filterAndLog_0_Or3Plus(FQCN, <span class="keyword">null</span>, Level.INFO, msg, <span class="keyword">null</span>, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">info</span><span class="params">(String format, Object arg)</span> </span>&#123;</span><br><span class="line">  filterAndLog_1(FQCN, <span class="keyword">null</span>, Level.INFO, format, arg, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">info</span><span class="params">(String format, Object arg1, Object arg2)</span> </span>&#123;</span><br><span class="line">  filterAndLog_2(FQCN, <span class="keyword">null</span>, Level.INFO, format, arg1, arg2, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">info</span><span class="params">(String format, Object[] argArray)</span> </span>&#123;</span><br><span class="line">  filterAndLog_0_Or3Plus(FQCN, <span class="keyword">null</span>, Level.INFO, format, argArray, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">info</span><span class="params">(String msg, Throwable t)</span> </span>&#123;</span><br><span class="line">  filterAndLog_0_Or3Plus(FQCN, <span class="keyword">null</span>, Level.INFO, msg, <span class="keyword">null</span>, t);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">info</span><span class="params">(Marker marker, String msg)</span> </span>&#123;</span><br><span class="line">  filterAndLog_0_Or3Plus(FQCN, marker, Level.INFO, msg, <span class="keyword">null</span>, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">......</span><br></pre></td></tr></table></figure></p>
<p>　　可以看到，作者实现了很多的以filterAndLog为前缀的方法，看了下各个方法，主要区别在调用了LoggerContext中以getTurboFilterChainDecision_为前缀的方法，看资料说是基于性能的考虑，此处不再做细究，忽略。不同的filterAndLog方法，最终都会调用buildLoggingEventAndAppend -&gt; callAppenders方法、实现如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">buildLoggingEventAndAppend</span><span class="params">(<span class="keyword">final</span> String localFQCN,</span><br><span class="line">    <span class="keyword">final</span> Marker marker, <span class="keyword">final</span> Level level, <span class="keyword">final</span> String msg,</span><br><span class="line">    <span class="keyword">final</span> Object[] params, <span class="keyword">final</span> Throwable t)</span> </span>&#123;</span><br><span class="line">  LoggingEvent le = <span class="keyword">new</span> LoggingEvent(localFQCN, <span class="keyword">this</span>, level, msg, t, params);</span><br><span class="line">  le.setMarker(marker);</span><br><span class="line">  callAppenders(le);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">callAppenders</span><span class="params">(ILoggingEvent event)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> writes = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">for</span> (Logger l = <span class="keyword">this</span>; l != <span class="keyword">null</span>; l = l.parent) &#123;</span><br><span class="line">    writes += l.appendLoopOnAppenders(event);</span><br><span class="line">    <span class="keyword">if</span> (!l.additive) &#123;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// No appenders in hierarchy</span></span><br><span class="line">  <span class="keyword">if</span> (writes == <span class="number">0</span>) &#123;</span><br><span class="line">    loggerContext.noAppenderDefinedWarning(<span class="keyword">this</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">appendLoopOnAppenders</span><span class="params">(ILoggingEvent event)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (aai != <span class="keyword">null</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> aai.appendLoopOnAppenders(event);</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　callAppenders方法会调用此Logger关联的所有Appender，而且还会调用所有父Logger关联的Appender，直到遇到父Logger的additive属性设置为false为止，这也是为什么如果子Logger和父Logger都关联了同样的Appender，则日志信息会重复记录的原因。<br>　　在appendLoopOnAppenders方法中，实际调用了AppenderAttachableImpl的appendLoopOnAppenders方法。在AppenderAttachableImpl中，保存了该Logger对应的所有Appender。可以将AppenderAttachableImpl理解为一个日志打印代理。</p>
<h2 id="u603B_u7ED3"><a href="#u603B_u7ED3" class="headerlink" title="总结"></a>总结</h2><ol>
<li>每个Logger都会通过成员变量aai（AppenderAttachableImpl类型）存储该Logger相关的所有Appender（不包括父Logger的Appender）</li>
<li>每次调用info、debug方法时，最终都会委托AppenderAttachableImpl的appendLoopOnAppenders方法进行处理。该方法实现如下，一个循环而已！<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">appendLoopOnAppenders</span><span class="params">(E e)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> size = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (Appender&lt;E&gt; appender : appenderList) &#123;</span><br><span class="line">      appender.doAppend(e);</span><br><span class="line">      size++;</span><br><span class="line">    &#125;</span><br><span class="line">  <span class="keyword">return</span> size;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://kyfxbl.iteye.com/blog/1170283" target="_blank" rel="external">http://kyfxbl.iteye.com/blog/1170283</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/15/log/8.logback系列之debug info等/" data-id="cijzzpvbl00ab1cfs21ju09fc" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/log/">log</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/logback/">logback</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-log/7.logback系列之LoggerContext、Logger" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/15/log/7.logback系列之LoggerContext、Logger/" class="article-date">
  <time datetime="2015-12-14T23:00:00.000Z" itemprop="datePublished">2015-12-15</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/log/">log</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2015/12/15/log/7.logback系列之LoggerContext、Logger/">logback系列三之LoggerContext、Logger</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="StaticLoggerBinder"><a href="#StaticLoggerBinder" class="headerlink" title="StaticLoggerBinder"></a>StaticLoggerBinder</h2><p>　　我们进入StaticLoggerBinder类，其有一个成员变量LoggerContext，该类实现了org.slf4j.ILoggerFactory，（在slf4j-log4j中则是由Log4jLoggerFactory实现org.slf4j.ILoggerFactory），它扮演了LoggerFactory的功能，所以其最核心的方法是getLogger(String name)。<br>　　虽然LoggerContext没有显式地采用单例模式，但实际上应用从来不会直接获取到LoggerContext类的实例，都是通过StaticLoggerBinder内的defaultLoggerContext字段来间接引用LoggerContext的实例，而StaticLoggerBinder本身是单例的，所以程序中用到的LoggerContext也始终是同一个实例，这点很重要。</p>
<h2 id="LoggerContext"><a href="#LoggerContext" class="headerlink" title="LoggerContext"></a>LoggerContext</h2><p>　　在log4j中，初始化的工作，如配置文件解析，主要是由LogManager完成。而在logback中则主要是由LoggerContext处理。在LoggerContext的构造方法中，初始化了rootLogger（在log4j中则是由LogManager的静态代码块完成）。下面贴出LoggerContext的getLogger方法。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> Logger <span class="title">getLogger</span><span class="params">(<span class="keyword">final</span> String name)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (name == <span class="keyword">null</span>) &#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"name argument cannot be null"</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(name)) &#123;</span><br><span class="line">    <span class="keyword">return</span> root;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">  Logger logger = root;</span><br><span class="line"></span><br><span class="line">  Logger childLogger = (Logger) loggerCache.get(name);</span><br><span class="line">  <span class="keyword">if</span> (childLogger != <span class="keyword">null</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> childLogger;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  String childName;</span><br><span class="line">  <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">    <span class="keyword">int</span> h = LoggerNameUtil.getSeparatorIndexOf(name, i);</span><br><span class="line">    <span class="keyword">if</span> (h == -<span class="number">1</span>) &#123;</span><br><span class="line">      childName = name;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      childName = name.substring(<span class="number">0</span>, h);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// move i left of the last point</span></span><br><span class="line">    i = h + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">synchronized</span> (logger) &#123;</span><br><span class="line">      childLogger = logger.getChildByName(childName);</span><br><span class="line">      <span class="keyword">if</span> (childLogger == <span class="keyword">null</span>) &#123;</span><br><span class="line">        childLogger = logger.createChildByName(childName);</span><br><span class="line">        loggerCache.put(childName, childLogger);</span><br><span class="line">        incSize();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    logger = childLogger;</span><br><span class="line">    <span class="keyword">if</span> (h == -<span class="number">1</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> childLogger;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　从上面代码可以看出，其返回一个Logger的关键步骤主要有<br>　　1. 如果name是null，抛出异常，不过这个情况基本是不会发生的<br>　　2. 如果请求的是ROOT Logger，那么就直接返回root<br>　　3. 检查一下请求的Logger是否已经创建过了，如果已经创建过，就直接从loggerCache中返回<br>　　4. 如果还没创建过，那就开始逐层创建，比如请求的Logger的name是com.shc.logback.Demo，那么一共会创建4个Logger，分别是Logger[com]、Logger[com.shc]、Logger[com.shc.logback]、Logger[com.shc.logback.Demo]，下面我们将详细介绍该步骤。</p>
<p>　　第一次进入while循环时，logger为root，查看该root是否存在名为com的child，如果没有则创建，并放入loggerCache，再调用incSize使logger的数据加一，最后将刚创建的childLogger赋值给logger<br>　　再次进入while循环时，此时logger为上一步创建的名称为com的logger，判断其存在名为com.shc的child，重复上面的步骤直至退出循环。<br>　　这里需要重点说明一下，getChildByName和createChildByName两个方法。在Logger中，维护了一个类型为List<logger>的属性childrenList，getChildByName就是通过循环该list，查看是否存在子logger。在createChildByName方法中根据该参数名新建一个子Logger，并放入父Logger的childrenList中。并且Logger的构造函数的第二个参数为父Logger对象。<br>　　由此，我们可以看出，<strong>Logger自身就维护了父子的关系，这样就为logger的继承关系打下了简单精巧的基础</strong>，大赞，基本上甩log4j九条街啊！<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Logger <span class="title">getChildByName</span><span class="params">(<span class="keyword">final</span> String childName)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (childrenList == <span class="keyword">null</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">int</span> len = <span class="keyword">this</span>.childrenList.size();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">      <span class="keyword">final</span> Logger childLogger_i = (Logger) childrenList.get(i);</span><br><span class="line">      <span class="keyword">final</span> String childName_i = childLogger_i.getName();</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (childName.equals(childName_i)) &#123;</span><br><span class="line">        <span class="keyword">return</span> childLogger_i;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// no child found</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">Logger <span class="title">createChildByName</span><span class="params">(<span class="keyword">final</span> String childName)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> i_index = LoggerNameUtil.getSeparatorIndexOf(childName, <span class="keyword">this</span>.name.length() + <span class="number">1</span>);</span><br><span class="line">  <span class="keyword">if</span> (i_index != -<span class="number">1</span>) &#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"For logger ["</span> + <span class="keyword">this</span>.name</span><br><span class="line">        + <span class="string">"] child name ["</span> + childName</span><br><span class="line">        + <span class="string">" passed as parameter, may not include '.' after index"</span></span><br><span class="line">        + (<span class="keyword">this</span>.name.length() + <span class="number">1</span>));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (childrenList == <span class="keyword">null</span>) &#123;</span><br><span class="line">    childrenList = <span class="keyword">new</span> ArrayList&lt;Logger&gt;(DEFAULT_CHILD_ARRAY_SIZE);</span><br><span class="line">  &#125;</span><br><span class="line">  Logger childLogger;</span><br><span class="line">  childLogger = <span class="keyword">new</span> Logger(childName, <span class="keyword">this</span>, <span class="keyword">this</span>.loggerContext);</span><br><span class="line">  childrenList.add(childLogger);</span><br><span class="line">  childLogger.effectiveLevelInt = <span class="keyword">this</span>.effectiveLevelInt;</span><br><span class="line">  <span class="keyword">return</span> childLogger;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></logger></p>
<h2 id="u603B_u7ED3"><a href="#u603B_u7ED3" class="headerlink" title="总结"></a>总结</h2><p>　　通过上面的分析，可以看到</p>
<ol>
<li>LoggerContext通过持有Root Logger，从而拥有遍历整个logger树的能力</li>
<li>每个Logger均持有属性，name（名称）、level（级别）、effectiveLevelInt（该Logger的生效级别，会从父Logger继承得到）</li>
<li>每个Logger均持还有两个非常重要的属性，parent和childrenList，这样就实现了Logger的层次结构，形成了父子关系</li>
<li>最顶层的父类会在LoggerContext的getLogger方法中作为RootLogger的子Logger</li>
<li>LoggerContext通过持有RootLogger便可以获取到任意一个logger，好简单精巧的设计！<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><a href="http://kyfxbl.iteye.com/blog/1170283" target="_blank" rel="external">http://kyfxbl.iteye.com/blog/1170283</a><br><a href="http://kyfxbl.iteye.com/category/174620" target="_blank" rel="external">http://kyfxbl.iteye.com/category/174620</a></li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/15/log/7.logback系列之LoggerContext、Logger/" data-id="cijzzpvbp00af1cfss7n5xazw" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/log/">log</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/logback/">logback</a></li></ul>

    </footer>
  </div>
  
</article>


  
  
    <nav id="page-nav">
      <a class="extend prev" rel="prev" href="/page/9/">&laquo; Prev</a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/8/">8</a><a class="page-number" href="/page/9/">9</a><span class="page-number current">10</span><a class="page-number" href="/page/11/">11</a><a class="page-number" href="/page/12/">12</a><span class="space">&hellip;</span><a class="page-number" href="/page/15/">15</a><a class="extend next" rel="next" href="/page/11/">Next &raquo;</a>
    </nav>
  
</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/cache/">cache</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/database/">database</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/docker/">docker</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gis/">gis</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/go/">go</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gradle/">gradle</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/groovy/">groovy</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hadoop/">hadoop</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/java/">java</a><span class="category-list-count">18</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/linux/">linux</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/log/">log</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mongodb/">mongodb</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mq/">mq</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mybatis/">mybatis</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/netty/">netty</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nginx/">nginx</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nio/">nio</a><span class="category-list-count">19</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nodejs/">nodejs</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/redis/">redis</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/rpc/">rpc</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/spring/">spring</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/tomcat/">tomcat</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/zookeeper/">zookeeper</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/杂项/">杂项</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/aop/">aop</a><span class="tag-list-count">8</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/aspectj/">aspectj</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cache/">cache</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/classloader/">classloader</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/common-loggings/">common-loggings</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/commonjs/">commonjs</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/database/">database</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/dubbo/">dubbo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fastdfs/">fastdfs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gclib/">gclib</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/geohash/">geohash</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gis/">gis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gradle/">gradle</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/groovy/">groovy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hadoop/">hadoop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hessisan/">hessisan</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">48</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/javassist/">javassist</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/kafka/">kafka</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log/">log</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log4j/">log4j</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logback/">logback</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/map/">map</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/memecached/">memecached</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mina/">mina</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongodb/">mongodb</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mq/">mq</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mqtt/">mqtt</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mybatis/">mybatis</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/netty/">netty</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/">nginx</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nio/">nio</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nodejs/">nodejs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/postgresql/">postgresql</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/">protobuf</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/proxy/">proxy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/push/">push</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rabbitmq/">rabbitmq</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/">redis</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rpc/">rpc</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sitemesh/">sitemesh</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sld/">sld</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/slf4j/">slf4j</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/socket/">socket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spatialite-for-android/">spatialite for android</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spi/">spi</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/">spring</a><span class="tag-list-count">13</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tomcat/">tomcat</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/transaction/">transaction</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/web/">web</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/xSocket/">xSocket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zbus/">zbus</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zookeeper/">zookeeper</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/数据结构/">数据结构</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/aop/" style="font-size: 15.38px;">aop</a> <a href="/tags/aspectj/" style="font-size: 11.54px;">aspectj</a> <a href="/tags/cache/" style="font-size: 11.54px;">cache</a> <a href="/tags/classloader/" style="font-size: 10px;">classloader</a> <a href="/tags/common-loggings/" style="font-size: 10px;">common-loggings</a> <a href="/tags/commonjs/" style="font-size: 12.31px;">commonjs</a> <a href="/tags/database/" style="font-size: 16.92px;">database</a> <a href="/tags/docker/" style="font-size: 13.85px;">docker</a> <a href="/tags/dubbo/" style="font-size: 10px;">dubbo</a> <a href="/tags/fastdfs/" style="font-size: 10px;">fastdfs</a> <a href="/tags/gclib/" style="font-size: 10.77px;">gclib</a> <a href="/tags/geohash/" style="font-size: 10px;">geohash</a> <a href="/tags/gis/" style="font-size: 10px;">gis</a> <a href="/tags/git/" style="font-size: 10.77px;">git</a> <a href="/tags/go/" style="font-size: 10px;">go</a> <a href="/tags/gradle/" style="font-size: 10px;">gradle</a> <a href="/tags/groovy/" style="font-size: 10px;">groovy</a> <a href="/tags/hadoop/" style="font-size: 10px;">hadoop</a> <a href="/tags/hessisan/" style="font-size: 11.54px;">hessisan</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/javassist/" style="font-size: 10px;">javassist</a> <a href="/tags/kafka/" style="font-size: 11.54px;">kafka</a> <a href="/tags/linux/" style="font-size: 12.31px;">linux</a> <a href="/tags/log/" style="font-size: 17.69px;">log</a> <a href="/tags/log4j/" style="font-size: 12.31px;">log4j</a> <a href="/tags/logback/" style="font-size: 13.08px;">logback</a> <a href="/tags/map/" style="font-size: 10px;">map</a> <a href="/tags/memecached/" style="font-size: 10.77px;">memecached</a> <a href="/tags/mina/" style="font-size: 13.08px;">mina</a> <a href="/tags/mongodb/" style="font-size: 19.23px;">mongodb</a> <a href="/tags/mq/" style="font-size: 13.85px;">mq</a> <a href="/tags/mqtt/" style="font-size: 10.77px;">mqtt</a> <a href="/tags/mybatis/" style="font-size: 16.15px;">mybatis</a> <a href="/tags/netty/" style="font-size: 10px;">netty</a> <a href="/tags/nginx/" style="font-size: 11.54px;">nginx</a> <a href="/tags/nio/" style="font-size: 13.08px;">nio</a> <a href="/tags/nodejs/" style="font-size: 10px;">nodejs</a> <a href="/tags/postgresql/" style="font-size: 10.77px;">postgresql</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/proxy/" style="font-size: 10px;">proxy</a> <a href="/tags/push/" style="font-size: 10px;">push</a> <a href="/tags/rabbitmq/" style="font-size: 11.54px;">rabbitmq</a> <a href="/tags/redis/" style="font-size: 13.85px;">redis</a> <a href="/tags/rpc/" style="font-size: 19.23px;">rpc</a> <a href="/tags/sitemesh/" style="font-size: 10px;">sitemesh</a> <a href="/tags/sld/" style="font-size: 10px;">sld</a> <a href="/tags/slf4j/" style="font-size: 10px;">slf4j</a> <a href="/tags/socket/" style="font-size: 14.62px;">socket</a> <a href="/tags/spatialite-for-android/" style="font-size: 10px;">spatialite for android</a> <a href="/tags/spi/" style="font-size: 10px;">spi</a> <a href="/tags/spring/" style="font-size: 18.46px;">spring</a> <a href="/tags/tomcat/" style="font-size: 10.77px;">tomcat</a> <a href="/tags/transaction/" style="font-size: 10px;">transaction</a> <a href="/tags/web/" style="font-size: 10px;">web</a> <a href="/tags/xSocket/" style="font-size: 14.62px;">xSocket</a> <a href="/tags/zbus/" style="font-size: 10px;">zbus</a> <a href="/tags/zookeeper/" style="font-size: 10.77px;">zookeeper</a> <a href="/tags/数据结构/" style="font-size: 10.77px;">数据结构</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">67</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">December 2015</a><span class="archive-list-count">81</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">November 2015</a><span class="archive-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recents</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2016/01/30/资料/">(no title)</a>
          </li>
        
          <li>
            <a href="/2016/01/30/java/13.Classloader系列一/">ClassLoader系列一</a>
          </li>
        
          <li>
            <a href="/2016/01/29/hadoop/1.hadoop系列之源码编译/">hadoop系列一之源码编译</a>
          </li>
        
          <li>
            <a href="/2016/01/29/go/1.GO系列之入门/">Go系列一之入门</a>
          </li>
        
          <li>
            <a href="/2016/01/28/tomcat/2.tomcat性能优化/">tomcat性能优化</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>